package back;

import back.exception.MyException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.io.StringWriter;

public class MyExceptionResolve implements HandlerExceptionResolver {
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {

//        request.setAttribute("msg",ex.getMessage());
        String msg;
        if (ex instanceof MyException){
           msg=ex.getMessage();
            System.out.println("msg = " + msg);
        }else {
            StringWriter out = new StringWriter();
            PrintWriter s = new PrintWriter(out);
            ex.printStackTrace(s);
            String sysMsg=out.toString();
            msg="网络错误";
            System.out.println("sysMsg = " + sysMsg);
            System.out.println("msg = " + msg);
        }

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.setViewName("/error/500");

        return modelAndView;
    }
}
